package flc.ast.utils;

import android.content.Context;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
 
public class OnSwipeTouchListener implements View.OnTouchListener {
 
    private final GestureDetector gestureDetector;
 
    public OnSwipeTouchListener(Context context) {
        gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                float distanceX = e2.getX() - e1.getX();
                float distanceY = e2.getY() - e1.getY();
 
                if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > 150) {
                    if (distanceX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                    return true;
                }
                else if (Math.abs(distanceY) > Math.abs(distanceX) && Math.abs(distanceY) > 150) {
                    if (distanceY > 0) {
                        onSwipeDown();
                    } else {
                        onSwipeUp();
                    }
                    return true;
                }
                return false;
            }
        });
    }
 
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }
 
    public void onSwipeRight() {
        // 右滑
    }
 
    public void onSwipeLeft() {
        // 左滑
    }
 
    public void onSwipeUp() {
        // 上滑
    }
 
    public void onSwipeDown() {
        // 下滑
    }
}
